/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ifpb.pp.rh.managedbean;

import ifpb.pp.rh.dao.DAO;
import ifpb.pp.rh.dao.DAOJPA;
import ifpb.pp.rh.entity.Cargo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


/**
 *
 * @author Gabriella
 */
@Controller
public class CargoManaged {

    @Autowired
    private DAO dao;
    private Cargo cargo;
    private DataModel model;

    public CargoManaged() {       
//        this.dao = new DAOJPA();
    }

    public Cargo getCargo() {
        return cargo;
    }

    public void setCargo(Cargo cargo) {
        this.cargo = cargo;
    }

     public String novoCargo(){
         this.cargo = new Cargo();
        return "cargoCriado";
    }

    public String cadastrarCargo(){
        try {            
            if (this.dao.salvar(cargo)) {
                this.novoCargo();
                return "cargoCadastrado";
            }
        } catch (Exception ex) {
           ex.printStackTrace();
        }
        return "erroCargoCadastrado";
    }

    public Cargo getCargoModificar(){
        Cargo cargoEscolhido = (Cargo) this.model.getRowData();
        return cargoEscolhido;

    }

    public DataModel getListaDeCargos(){
        List<Cargo> listaCargos;
        try {
            listaCargos = (List<Cargo>) this.dao.buscarTodos(Cargo.class);
            this.model = new ListDataModel(listaCargos);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return this.model;
    }

    public String editarCargo(){
        Cargo c = getCargoModificar();
        this.setCargo(c);
        return "editarCargo";
    }

    public String atualizarCargo(){
        Boolean resposta = false;
        try {
            resposta = this.dao.atualizar(this.cargo);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if(resposta)
            return "listarCargos";
        return "ErroAtualizarCargo";

    }

     public String remover() {
        try {

            Cargo cargo = getCargoModificar();            
             Map<Integer, Object> param = new HashMap<Integer, Object>();
            param.put(1, cargo.getNome());
            Cargo novoCargo = this.dao.consultaSimples("cargo.nome", param);
            if (dao.remover(novoCargo)) {
                return "listarCargos";
            }
        } catch (Exception ex) {            
            ex.printStackTrace();
        }
        return "erroListarCargo";
    }

}
